package com.paiai.lslease.action.appuser;

import com.paiai.lslease.auto.model.AppMessageCode;
import com.paiai.lslease.auto.model.AppSysUser;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.service.appuser.AppMessageCodeService;
import org.apache.commons.lang.StringUtils;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

public class AppMessageCodeAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(AppMessageCodeAction.class);

    private AppMessageCodeService appMessageCodeService;
    private String userMobile;
    private String messageCode;

    public void setAppMessageCodeService(AppMessageCodeService appMessageCodeService) {
        this.appMessageCodeService = appMessageCodeService;
    }

    public void setUserMobile(String userMobile) {
        this.userMobile = userMobile;
    }

    public void setMessageCode(String messageCode) {
        this.messageCode = messageCode;
    }

    /**
     * 用户注册获取验证码
     */
    public void getMessageCode() {
        try {
            if (TextUtils.isBlank(userMobile)) {
                sendFailureMsg("用户手机号码不能为空");
                return;
            }
            if (userMobile.length() != 11) {
                sendFailureMsg("用户手机号码必须为11位数字");
                return;
            }
            String message;
            message = appMessageCodeService.getMessageCode(userMobile);
            if (!StringUtils.isNumeric(message)) {
                sendFailureMsg(message);
                return;
            }
            Map<String, String> dataMap = new HashMap<String, String>();
            dataMap.put("messageCode", message);
            sendSuccesseMsg(dataMap, "用户注册获取验证码成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("用户注册获取验证码失败");
        }
    }

    /**
     * 检查验证码是否正确
     */
    public void checkMessageCode() {
        try {
            if (TextUtils.isBlank(userMobile)) {
                sendFailureMsg("用户手机号码不能为空");
                return;
            }
            if (userMobile.length() != 11) {
                sendFailureMsg("用户手机号码必须为11位数字");
                return;
            }
            if (TextUtils.isBlank(messageCode)) {
                sendFailureMsg("验证码不能为空");
                return;
            }
            String checkResult;
            AppMessageCode appMessageCode = new AppMessageCode();
            appMessageCode.setUserMobileAccount(userMobile);
            appMessageCode.setUserShortNum(messageCode);
            checkResult = appMessageCodeService.checkMessageCode(appMessageCode);
            if (!StringUtils.isBlank(checkResult)) {
                sendFailureMsg(checkResult);
                return;
            }
            sendSuccesseMsg("验证码校验成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("验证码校验失败");
        }
    }
}
